@echo off
echo This will do (or try) the following:
echo - kill all running instances of explorer.exe
echo - then unregister gisgeb (if it already exists)
echo - remove all traces of %PROGRAMFILES%\gisgeb
echo - re-create %PROGRAMFILES%\gisgeb from current ../bin
echo   (so make sure every thing is compiled...)
echo - register gisgeb (as a com server)
echo - restart an instance of explorer.exe
echo You need ADMINISTRATOR privileges to do this.
echo.
echo use this at your own risk
echo.
echo     YOU HAVE BEEN WARNED...
echo.
echo (press some key to proceed...)
pause

::find regasm.exe
REG.exe Query HKLM\Hardware\Description\System\CentralProcessor\0 | findstr /i "x86" > NUL
If %ERRORLEVEL% == 0 (
    SET BITS=32
) ELSE (
    SET BITS=64
)

echo. > _framework.txt
FOR /f "usebackq delims=\ tokens=5" %%G IN (`reg.exe query HKLM\Software\Microsoft\.NetFramework`) DO echo %%G >> _framework.txt
FOR /f "usebackq tokens=1" %%G IN (`findstr /i v4 _framework.txt`) DO SET Version=%%G
del _framework.txt

set regasm=C:\Windows\Microsoft.NET\Framework%BITS:32=%\%Version%\regasm.exe
if exist %regasm% goto start
echo RegAsm could not be found.
echo I detected %BITS%bits
echo Net Framework 4 is: %Version% 
echo So I guesst msbuild to be at: %regasm% 
echo but it is not :-( 
echo I can not proceed, you got to fix this...
echo. 
goto error

:start 
set gisgeb=%PROGRAMFILES%\gisgeb\gisgeb.comserver.dll

echo trying to kill explorer.exe... (could take some time...)
TASKKILL /F /IM "explorer.exe"
:: wait 5 secs or so
ping 127.0.0.1 -n 6 -w 1000 > nul

if not exist "%gisgeb%" goto no_gisgeb
echo.
echo "%gisgeb%" exist.
echo Trying to de-register
%regasm% "%gisgeb%" /unregister
goto del_folders

:no_gisgeb
echo.
echo "%gisgeb%" does not exist.
echo so we do not de-register

:del_folders
if not exist "%PROGRAMFILES%\gisgeb" goto no_folders
echo.
echo deleting
del /F /S /Q "%PROGRAMFILES%\gisgeb"
rmdir "%PROGRAMFILES%\gisgeb\plugins"
rmdir "%PROGRAMFILES%\gisgeb"
:: I hate batch-ing..
del /F /S /Q "%PROGRAMFILES%\gisgeb"
rmdir "%PROGRAMFILES%\gisgeb\plugins"
rmdir "%PROGRAMFILES%\gisgeb"

goto create_gisgeb

:no_folders
echo.
echo "%PROGRAMFILES%\gisgeb" does not exist.
echo so we do not delete

:create_gisgeb
echo.
echo copying data to "%PROGRAMFILES%\gisgeb"
MKDIR "%PROGRAMFILES%\gisgeb\plugins"
copy ..\..\bin\*.* "%PROGRAMFILES%\gisgeb\"
copy ..\..\bin\plugins\*.* "%PROGRAMFILES%\gisgeb\plugins"

echo.
echo registering "%gisgeb%"
%regasm% "%gisgeb%" /codebase

:end
echo.
echo we have ended. press some key to try and resart explorer.exe
pause
start explorer.exe
goto finish

:error
echo.
echo THERE WERE ERRORS !!

:finish
echo.
echo finished - press some key to exit.
pause